#include "Mobile.h"

void Mobile::update(float elapsedTime, IMap const & map)
{
    _gc.Update();
    _gc.Set_Statut(_vel.x != 0 ? "move" : "idle");

    // Gravity
    _vel.y = _vel.y + G;

    // slowing down
    if (_vel.x < 0)
        _vel.x = (_vel.x > -1 * Mobile::SLOWCOEFF ? 0 : _vel.x + Mobile::SLOWCOEFF);
    if (_vel.x > 0)
        _vel.x = (_vel.x < Mobile::SLOWCOEFF ? 0 : _vel.x - Mobile::SLOWCOEFF);

    GameObject::update(elapsedTime, map);
}

void Mobile::move(direction dir)
{
    if (dir & _collide) return;
    if (dir != _dir) _gc.flip(dir == GameObject::LEFT);
    _vel.x = Mobile::MOVESPEED * (dir == GameObject::RIGHT ? 1 : -1);
    _dir = dir;
}

void Mobile::jump()
{
    if (!(_collide & GameObject::DOWN)) return;
    _vel.y = Mobile::JUMPSPEED;
}

void Mobile::onHit()
{
    _active = --_hp > 0;
}
